package com.bazaaroid.server.gae.api.resources.partner;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import com.bazaaroid.server.gae.core.dao.partner.LocationDAO;
import com.bazaaroid.server.gae.core.dto.partner.AllEntitiesDTO;
import com.bazaaroid.server.gae.core.dto.partner.LocationDTO;
import com.bazaaroid.server.gae.core.dto.partner.AllEntitiesDTO.LocationsDTO;
import com.bazaaroid.server.gae.core.ioc.GuiceFactory;
import com.bazaaroid.server.gae.core.persistence.model.partner.Location;
import com.bazaaroid.server.gae.core.persistence.model.partner.Partner;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@Path("/location/")
public class LocationResource {

	@GET
	@Produces( { "application/xml", "application/json" })
	@Path("/{partnerId}/{locationId}")
	public LocationDTO getById(
			@PathParam ("partnerId") Long partnerId,
			@PathParam ("locationId") Long locationId) {
		Key partnerKey = KeyFactory.createKey(
				Partner.class.getSimpleName(), partnerId);
		Key locationKey = KeyFactory.createKey(
				partnerKey, Location.class.getSimpleName(), locationId);
		LocationDAO locationDAO = GuiceFactory.getInjector()
			.getInstance(LocationDAO.class);
		Location location = locationDAO.getById(locationKey);
		return new LocationDTO(location);
	}

	@GET
	@Produces( { "application/xml", "application/json" })
	@Path("/all/")
	public LocationsDTO getAllLocations() {
		LocationDAO locationDAO = GuiceFactory.getInjector()
			.getInstance(LocationDAO.class);
		List<Location> locations = locationDAO.getAll();
		return new AllEntitiesDTO.LocationsDTO(locations);
	}
}
